package com.eight.group.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eight.group.entity.QueryPageBean;
import com.eight.group.entity.Result;
import com.eight.group.utils.GetNameUtils;
import com.eight.group.utils.LocalPictureUtils;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.xqx.eight.group.service.MemberruploadService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/member_upload")
public class MemberuploadController {
    @Reference
    private MemberruploadService service;
    @RequestMapping("findAll.do")
    public List<Map<String,Object>> findAll(@RequestBody  QueryPageBean bean){
        List<Map<String, Object>> mapList = service.findAll(bean);
        return mapList;
    }
    @RequestMapping("upload.do")
    public Result upload(MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        String name = GetNameUtils.lihuan(filename);
        //文件名称  :会员手机号_体检日期_套餐id
        String newname= FilenameUtils.getName(name);
        //执行上传
        LocalPictureUtils.upload(newname, file.getBytes());
        return new Result(true, "上传文件成功!",newname);
    }
    //查询stmeal里面的信息
    @RequestMapping("findStmeal.do")
    public List<Map<String,Object>> findStmeal(){
        List<Map<String, Object>> stmeal = service.findStmeal();
        return stmeal;
    }
    @RequestMapping("edit.do")
    public Result edit(@RequestBody Map<String,Object> map){
        try {
            service.edit(map);
            return new Result(true, "修改成功!");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "修改失败!");
        }

    }
    @RequestMapping("add.do")
    public Result add(@RequestBody Map<String,Object> map){
       // System.out.println(""+map);
        try {
            Result result = service.add(map);
            return result;
        }catch (Exception e){
            return new Result(false,"添加失败" );
        }
    }
    @RequestMapping("delete.do")
    public Result delete(int id){
            try {
                service.delete(id);
                return new Result(true, "删除成功!");
            }catch (Exception e){
                return new Result(false, "删除失败！");
            }
    }
}
